package com.sczmgk.hotline_pzh.photo;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.view.CustomSecectPicImageView;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

/**
 * 相册的GreadView条目View
 *
 * @author bo
 *
 */
public class PhotoGridItem extends RelativeLayout implements Checkable {
	private Context mContext;
	/***
	 * 是否已千选择
	 */
	private boolean mCheck;
	/**
	 * 显示图片
	 */
	private ImageView mImageView;
	/**
	 * 图片是否选择的图标
	 */
	private CustomSecectPicImageView mPicSelect;
	/**
	 * 图片选择后的（半透明）背景
	 */
	private View mSelectedBg;
	
	public PhotoGridItem(Context context,int width,int height) {
		super(context, null, 0);
		mContext = context;
		LayoutInflater.from(mContext).inflate(R.layout.photoalbum_gridview_item, this);
		mImageView = (ImageView) findViewById(R.id.photo_img_view);
		mPicSelect = (CustomSecectPicImageView) findViewById(R.id.iv_select_pic);
	
		mSelectedBg = findViewById(R.id.ll_selected_bg);
		FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) mSelectedBg.getLayoutParams();
		layoutParams.width = width;
		layoutParams.height = height;
		mSelectedBg.setLayoutParams(layoutParams);
		
		mImageView.setScaleType(ScaleType.CENTER_CROP);
		layoutParams = (FrameLayout.LayoutParams) mImageView.getLayoutParams();
		layoutParams.width = width;
		layoutParams.height = height;
		mImageView.setLayoutParams(layoutParams);
		
		
	}
	
	@Override
	public void setChecked(boolean checked) {
		mCheck = checked;
		if(mCheck){
			mPicSelect.setChecked(true);
			mSelectedBg.setVisibility(View.VISIBLE);
		}else{
			mSelectedBg.setVisibility(View.GONE);
			mPicSelect.setChecked(false);
		}
	}
	/**
	 * 取得点击看大图片的图标
	 * @return
	 */
	public ImageView selectedPhotoImageView(){
		return mPicSelect;
	}
	
	@Override
	public boolean isChecked() {
		return mCheck;
	}

	@Override
	public void toggle() {
		setChecked(!mCheck);
	}

	public void setImgResID(int id) {
		if (mImageView != null) {
			mImageView.setImageResource(id);
		}
	}

	public void setImageBitmap(Bitmap bit) {
		if (mImageView != null) {
			mImageView.setImageBitmap(bit);
		}
	}
	
	public ImageView getImageView(){
		return mImageView;
	}

}
